package com.atlin.project.bizmq.bi;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import lombok.extern.slf4j.Slf4j;

/**
 * 用于创建测试程序用到的交换机和队列（只用在程序启动前执行一次）
 */
@Slf4j
public class BiInitMain {

    public static void initRabbitMQ() {
        try {
            log.info("initRabbitMQ ...");
            // 创建连接工厂
            ConnectionFactory factory = new ConnectionFactory();
//            factory.setHost("106.54.38.14");
//            factory.setUsername("admin");
//            factory.setPassword("atlin123456");
            factory.setHost("127.0.0.1");
//            factory.setUsername("admin");
//            factory.setPassword("atlin123456");
            // 创建连接
            Connection connection = factory.newConnection();
            // 创建通道
            Channel channel = connection.createChannel();

            // 声明交换机，指定交换机类型为 direct
            channel.exchangeDeclare(BiMqConstants.BI_EXCHANGE_NAME, "direct");

            // 声明队列，设置队列持久化、非独占、非自动删除，并传入额外的参数为 null
            channel.queueDeclare(BiMqConstants.BI_QUEUE_NAME, true, false, false, null);
            // 将队列绑定到交换机，指定路由键为 "my_routingKey"
            channel.queueBind(BiMqConstants.BI_QUEUE_NAME, BiMqConstants.BI_EXCHANGE_NAME, BiMqConstants.BI_ROUTING_KEY);
            log.info("initRabbitMQ successful");
        } catch (Exception e) {
            log.error("initRabbitMQ failure", e);
        }
    }

    public static void main(String[] args) {
        initRabbitMQ();
    }
}
